package edu.suda.cxtz.utils;

import com.github.dozermapper.core.Mapper;
import com.github.dozermapper.core.MappingProcessor;
import com.github.dozermapper.spring.DozerBeanMapperFactoryBean;
import edu.suda.cxtz.entity.User;
import edu.suda.cxtz.entity.dto.UserDto;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Component
@RequiredArgsConstructor
public class DtoEntityUtil {

    private final Mapper mapper;

    public <D, E> E trans(D t, Class<E> clazz) {
        if (t == null) {
            return null;
        }
        return mapper.map(t, clazz);
    }

    public <D, E> List<E> trans(Iterable<D> ts, Class<E> clazz) {
        List<E> es = new ArrayList<>(); // 使用<>代替显式类型E
        if (ts == null) {
            return es;
        }

        for (D d : ts) {
            E e = trans(d, clazz); // 直接使用E而不再进行转换
            if (e != null) {
                es.add(e);
            }
        }

        return es;
    }
}
